﻿@model IEnumerable<BaseData.Model.User>
@{
    ViewBag.Title = "用户管理";
    var idx = 0;
}

@*@Html.Partial("~/Views/Shared/_UserTitle.cshtml")*@
@*<div class="row">
    <div class="col-md-12">*@
        <div class="panel panel-default">
            <div class="panel-heading">
                <table style=" width:100%;">
                    <tr>
                        <td>
                            <h2 class="panel-title">系统用户管理</h2>
                        </td>
                        <td class="pull-right">
                            <a href="#" data-toggle="modal" data-backdrop="static" data-target="#myModal" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-plus"></span> 新建</a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="panel-body">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>
                                序号
                            </th>
                            <th>
                                登录账号
                            </th>
                            <th>
                                用户名称
                            </th>
                            <th>
                                所属区域
                            </th>
                            <th>
                                状态
                            </th>
                            <th>
                                操作
                            </th>
                        </tr>
                    </thead>
                    @foreach (var item in Model)
                    {
                        idx++;
                        <tr>
                            <td>
                                @idx
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.UserAccount)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.UserName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Area.City.CityName)-@Html.DisplayFor(modelItem => item.Area.AreaName)
                            </td>
                            <td>

                                @if (item.Enable == true)
                                {
                                    <span>正常</span>
                                }
                                else
                                {
                                    <span style=" color:red">已锁定</span>
                                }

                            </td>
                            <td>

                                <a title="编辑" data-placement="bottom" data-toggle="tooltip" href="javascript:Edit('@item.UserID');"><span class=" glyphicon glyphicon-edit"></span></a>
                                &nbsp;&nbsp;
                                <a title="删除" data-placement="bottom" data-toggle="tooltip" href="javascript:Delete('@item.UserID')"><span class="glyphicon glyphicon-trash"></span></a>
                                &nbsp;&nbsp;
                                @if (item.Enable == true)
                                {
                                    <a title="锁定" data-placement="bottom" data-toggle="tooltip" href="javascript:Lock('@item.UserID');"><span class=" glyphicon glyphicon-lock"></span></a>
                                }
                                else
                                {
                                    <a title="解除锁定" data-placement="bottom" data-toggle="tooltip" href="javascript:ReLock('@item.UserID');"><span class=" glyphicon glyphicon-lock"></span></a>
                                }


                            </td>
                        </tr>
                    }

                </table>
            </div>
        </div>
    @*</div>
</div>*@

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">用户新建</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">

                    <div class="form-group">
                        <label for="useraccount" class="col-sm-2 control-label">用户账号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="useraccount"
                                   placeholder="请输入用户账号,不少于6位">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="username" class="col-sm-2 control-label">用户姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="username"
                                   placeholder="请输入用户姓名">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="userpwd" class="col-sm-2 control-label">登陆密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="userpwd"
                                   placeholder="请输入登陆密码" maxlength="6">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="reuserpwd" class="col-sm-2 control-label">确认密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="reuserpwd"
                                   placeholder="请确认登陆密码" maxlength="6">
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="area" class="col-sm-2 control-label">所属区域</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="area">
                                <option value="">默认选择</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>

            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="errmsg">输出数据不合法，请检查重新输入！</div>

                <button type="button" class="btn btn-default" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
                <button id="save" type="button" class="btn btn-primary">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myEditModal" tabindex="-1" role="dialog" aria-labelledby="myEditModalLable" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myEditModalLable">用户修改</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">
                    <input type="hidden" id="userid" />
                    <div class="form-group">
                        <label for="eduseraccount" class="col-sm-2 control-label">用户账号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="eduseraccount"
                                   placeholder="请输入用户账号" disabled>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edusername" class="col-sm-2 control-label">用户姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edusername"
                                   placeholder="请输入用户姓名">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="eduserpwd" class="col-sm-2 control-label">登陆密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="eduserpwd"
                                   placeholder="请输入登陆密码" maxlength="6">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edreuserpwd" class="col-sm-2 control-label">确认密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="edreuserpwd"
                                   placeholder="请输入登陆密码" maxlength="6">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="earea" class="col-sm-2 control-label">所属区域</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="earea">
                                <option value="">默认选择</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="ederrmsg">输出数据不合法，请检查重新输入！</div>
                <button type="button" class="btn btn-default" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
                <button id="esave" type="button" class="btn btn-primary">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
            </div>
        </div>
    </div>
</div>


@section Scripts {
    <script>
        var authJson = null;
        $(function () {
            $("[data-toggle='tooltip']").tooltip();
            $.getJSON("/api/AreasApi", function (data) {
                var item = "<option value=-1>=请选择=</option>";
                for (var i = 0; i < data.length; i++) {
                    item += ' <option value="' + data[i].AreaID + '">' + data[i].City.CityName + '-' + data[i].AreaName + '</option>';
                }
                $("#area").empty().append(item);
                $("#earea").empty().append(item);
            });

            //新建
            $("#username").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#useraccount").bind('change', function () {
                $("#errmsg").css("display", "none")
                $.getJSON("/Users/CheckAccount", { account: $(this).val() }, function (data) {
                    if (data == "ishave") {
                        $("#errmsg").css("display", "block").text("账号:" + $("#useraccount").val() + "已被使用，请重新输入！");
                        $("#useraccount").val('').focus();
                    }
                });
            });
            $("#reuserpwd").bind('change', function () { $("#errmsg").css("display", "none"); $(this).parents(".form-group").attr("class", "form-group"); });
            $("#userpwd").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });

            //编辑
            $("#edusername").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });

            $("#edreuserpwd").bind('change', function () { $("#ederrmsg").css("display", "none"); $(this).parents(".form-group").attr("class", "form-group"); });
            $("#eduserpwd").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });

            $("#area").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#earea").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });

            $("#save").bind("click", function () {
                $("#errmsg").css("display", "none");
                var model = {};
                model.UserName = $("#username").val();
                model.UserAccount = $("#useraccount").val();
                model.Password = $("#reuserpwd").val();
                model.AreaID = $("#area").val();
                model.Enable = true;
                model.ID = 0;
               
                if (model.UserAccount == "") {
                    $("#useraccount").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.UserName == "") {
                    $("#username").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if ($("#userpwd").val() == "") {
                    $("#userpwd").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.Password == "") {
                    $("#reuserpwd").parents(".form-group").attr("class", "form-group has-error");
                    $("#errmsg").css("display", "none");
                    return;
                }
                if ($("#userpwd").val() != model.Password) {
                    $("#errmsg").css("display", "block").text("两次输入密码不一致！");
                    return;
                }
                if ($("#area").val() == "-1") {
                    $("#area").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                $.ajax({
                    url: "/Users/Create",
                    type: 'POST',
                    data: { jsonstr: JSON.stringify(model) },
                    success: function (data) {
                        if (data == "OK") {
                            location.href = "/Users/Index";
                        }
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#errmsg").css("display", "block");
                        }
                    }
                });
            });
        });
        function Edit(id) {
            $.ajax({
                url: "/Users/GetForEdit",
                type: 'GET',
                data: { id: id },
                success: function (data) {
                    $("#edusername").val(data.UserName);
                    $("#userid").val(data.UserID);
                    $("#eduseraccount").val(data.UserAccount);
                    $("#edreuserpwd").val(data.Password);
                    $("#eduserpwd").val(data.Password);
                    $("#earea").val(data.AreaID);
                    $('#myEditModal').modal('show');
                }
            });
            $("#esave").bind("click", function () {
                $("#ederrmsg").css("display", "none");
                var model = {};
                model.UserName = $("#edusername").val();
                model.UserAccount = $("#eduseraccount").val();
                model.Password = $("#edreuserpwd").val();
                model.Enable = true;
                model.UserID = $("#userid").val();
                model.AreaID = $("#earea").val();
                if (model.UserName == "") {
                    $("#edusername").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.UserAccount == "") {
                    $("#eduseraccount").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if ($("#eduserpwd").val() == "") {
                    $("#eduserpwd").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.Password == "") {
                    $("#edreuserpwd").parents(".form-group").attr("class", "form-group has-error");
                    $("#ederrmsg").css("display", "none");
                    return;
                }
                if ($("#eduserpwd").val() != model.Password) {
                    $("#ederrmsg").css("display", "block").text("两次输入密码不一致！");
                    return;
                }
                if ($("#earea").val() == "-1") {
                    $("#earea").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                $.ajax({
                    url: "/Users/Edit",
                    type: 'POST',
                    data: { jsonstr: JSON.stringify(model) },
                    success: function (data) {
                        location.href = "/Users/Index";
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#ederrmsg").css("display", "block");
                        }
                    }
                });
            });
        }
        function Delete(id) {
            if (confirm("提示：以下操作将会删除该用户信息，是否继续?")) {
                $.ajax({
                    url: "/Users/Delete",
                    type: 'GET',
                    data: { id: id },
                    success: function (data) {
                        location.href = "/Users/Index";
                    }
                });
            }
        }
        function Lock(id) {
            if (confirm("提示：以下操作将会锁定该账号，是否继续?")) {
                $.ajax({
                    url: "/Users/Lock",
                    type: 'GET',
                    data: { id: id },
                    success: function (data) {
                        location.href = "/Users/Index";
                    }
                });
            }
        }
        function ReLock(id) {
            if (confirm("提示：以下操作将会解除该账号锁定，是否继续?")) {
                $.ajax({
                    url: "/Users/Lock",
                    type: 'GET',
                    data: { id: id },
                    success: function (data) {
                        location.href = "/Users/Index";
                    }
                });
            }
        }
    </script>
}
